#include "LoopControllerConfigPage.h"

#include "ConfigManager.h"
#include "NumberEditor.h"
#include "LoopManager.h"


LoopControllerConfigPage::LoopControllerConfigPage(const int pageNum) : super(pageNum), mconfigManager(ConfigManagerSingleton::instance()) {
    //ctor
}

LoopControllerConfigPage::~LoopControllerConfigPage() {
    //dtor
}



void LoopControllerConfigPage::initEditors() {
    IntEditor* pEditor = new IntEditor(2, 0, 4, 100, 35000, 100);    //
    pEditor->format = "%5d";
    editUnits.push_back(BasicEditorSharedPtr(pEditor));
}



void LoopControllerConfigPage::setValue() {
    static_cast<IntEditor*> (editUnits[0].get())->setValue(mloopControllerConfig.switchInterval);
}


/**
 * 设置时间数据
 */
void LoopControllerConfigPage::saveValue() {
    mloopControllerConfig.switchInterval = static_cast<IntEditor*> (editUnits[0].get())->getValue();
    mconfigManager.getExtendDeviceConfig().loopControllerInfo = mloopControllerConfig;
    mconfigManager.saveExtendDeviceConfig();

    LoopManagerSingleton::instance().updateLoopControllerConfig(mloopControllerConfig);
}



void LoopControllerConfigPage::beforeDisplay() {
    mloopControllerConfig = mconfigManager.getExtendDeviceConfig().loopControllerInfo;
}



void LoopControllerConfigPage::paint() {
    if (focus) {
        EditablePage::paint();
    } else {
        std::string content[4];
        resetStream();
        strStream << "loopCtlConfig" << std::setw(3) << pageNum;
        content[0] = strStream.str();
        content[1] = "switchInterval:";
        resetStream();
        strStream <<  std::setw(5) << mloopControllerConfig.switchInterval << "ms";
        content[2] = strStream.str();
        screen.show(&content[0], &content[1], &content[2], &content[3]);
    }
}


